package service

import (
	"guard/internal/app/admin/models"
	"guard/internal/errors"
	"guard/internal/tool"
)

func (s *adminServiceImpl) ScopeAll() ([]models.AuthorityScope, error) {
	mas, err := s.st.SelScopeAll()
	if err != nil {
		return nil, errors.OtherError("获取scope失败")
	}
	return mas, nil
}

func (s *adminServiceImpl) ScopePage(current, size int) (*tool.Paginator, error) {
	scopePage := s.st.SelScopePage(current, size)
	if scopePage == nil {
		return nil, errors.OtherError("分页异常")
	}
	return scopePage, nil
}

func (s *adminServiceImpl) QueryScope(condition string) (mas []models.AuthorityScope, err error) {
	sq, err := s.st.SelScopeQuery(condition, condition)
	if err != nil {
		return nil, errors.OtherError("查询失败")
	}
	mas = append(mas, *sq)
	return mas, nil
}

func (s *adminServiceImpl) AddScope(asInfo *models.AuthorityScope) error {
	err := s.st.SaveOrUpdateScope(*asInfo)
	if err != nil {
		return errors.OtherError("保存失败")
	}
	return nil
}

func (s *adminServiceImpl) RemoveScope(tag string) error {
	err := s.st.DelScope(tag)
	return err
}
